; void pixelar_asm (
; 	unsigned char *src,
; 	unsigned char *dst,
; 	int m,
; 	int n,
; 	int src_row_size,
; 	int dst_row_size
; );

global pixelar_asm

pixelar_asm:
	;; TODO: Implementar
	push rbp
	mov rbp, rsp
	push rbx
	push r12
	push r13
	push r14
	push r15

	pxor xmm7, xmm7
	mov r13, rdi
	mov r14, rsi

	xor r12, r12
	cicloFilas:
		xor r11, r11
		cicloColumnas:
			pxor xmm0, xmm0

			lea r10, [r13 + r11]
			movd xmm0, [r10]		;xmm0 = p03|p02|p01|p00
			punpcklbw xmm0, xmm7		;xmm0 = 000|p03|000|p02|000|p01|000|p00
			add r10, r8

			movd xmm1, [r10]
			punpcklbw xmm1, xmm7		;xmm1 = 000|p13|000|p12|000|p11|000|p10
			paddw xmm0, xmm1
			add r10, r8

			movd xmm1, [r10]
			punpcklbw xmm1, xmm7		;xmm1 = 000|p23|000|p22|000|p21|000|p20
			paddw xmm0, xmm1
			add r10, r8

			movd xmm1, [r10]
			punpcklbw xmm1, xmm7		;xmm1 = 000|p33|000|p32|000|p31|000|p30
			paddw xmm0, xmm1		;xmm0 = 000|SS3|000|SS2|000|SS1|000|SS0
			add r10, r8

			movq xmm2, xmm0			;xmm2 = 000|SS3|000|SS2|000|SS1|000|SS0
			movq xmm3, xmm0			;xmm3 = 000|SS3|000|SS2|000|SS1|000|SS0

			psrlq xmm2, 16			;xmm2 = 000|000|000|SS3|000|SS2|000|SS1
			paddw xmm0, xmm2
			psrlq xmm2, 16			;xmm2 = 000|000|000|000|000|SS3|000|SS2
			paddw xmm0, xmm2
			psrlq xmm2, 16			;xmm2 = 000|000|000|000|000|000|000|SS3
			paddw xmm0, xmm2

			psllq xmm3, 16			;xmm3 = 000|SS2|000|SS1|000|SS0|000|000
			paddw xmm0, xmm3
			psllq xmm3, 16			;xmm3 = 000|SS1|000|SS0|000|000|000|000
			paddw xmm0, xmm3
			psllq xmm3, 16			;xmm3 = 000|SS0|000|000|000|000|000|000
			paddw xmm0, xmm3		;xmm0 = 000|SST|000|SST|000|SST|000|SST

			psrlw xmm0, 4
			packuswb xmm0, xmm7		;xmm0 = SST|SST|SST|SST

			lea r15, [r14 + r11]
			movd [r15], xmm0
			add r15, r9
			movd [r15], xmm0
			add r15, r9
			movd [r15], xmm0
			add r15, r9
			movd [r15], xmm0
		add r11, 4
		mov rax, r11
		add rax, 3
		cmp rax, rcx
		jl cicloColumnas

		lea r13, [r13 + r8 * 4]
		lea r14, [r14 + r9 * 4]
	add r12, 4
	mov rax, r12
	add rax, 3
	cmp rax, rdx
	jl cicloFilas

	pop r15
	pop r14
	pop r13
	pop r12
	pop rbx
	pop rbp
	ret
